import { getUserData } from '@/api/userData';
// import store from '@/store';

const userData = {
    state: {
        userList: [], // 用户动态路由
        userSelect: undefined, // 选中的用户
    },
    mutations: {
        SET_USER_LIST: (state, roles) => {
            state.userList = roles;
        },
        SET_USER_SELECT: (state, roles) => {
            state.userSelect = roles;
        },
    },
    actions: {
        // 获取用户信息
        GetUserInfo({ commit }) {
            return new Promise((resolve, reject) => {
                getUserData()
                    .then(result => {
                        // return store.dispatch('oidcStore/signOutOidc')

                        const userData = result.data;
                        let userAction = 0;
                        if (userData.length) {
                            userAction = 1;
                            for (
                                let index = 0;
                                index < userData.length;
                                index++
                            ) {
                                const element = userData[index];
                                if (element.userDisabled === 1) {
                                    userAction = 2;
                                    commit('SET_USER_SELECT', element);
                                    break;
                                } else {
                                    userAction = 1;
                                }
                            }
                        }
                        commit('SET_USER_LIST', userData);
                        resolve({
                            userAction: userAction,
                            userData: userData,
                        });
                    })
                    .catch(error => {
                        reject(error);
                    });
            });
        },
    },
};

export default userData;
